<?php
session_start();  //启动会话
//如果未登录，没有设置session变量“stuNo”，跳转到登录页
if (!isset($_SESSION['stuNo'])) {
    header('Location:Login.php?frompage=student');
}
?>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>学生信息</title>

</head>


<body>
    <?php
    include 'HeaderNav.html';  //包含页头与导航页
    ?>
    <h1>学生信息</h1>
    <?php
    //引用数据库连接文件
    require_once 'Conn.php';
    mysqli_query($db, 'set names utf8');


    @$allNum = allNews();
    @$pageSize = 3; //约定每页显示几条信息
    @$pageNum = empty($_GET["pageNum"]) ? 1 : $_GET["pageNum"];
    @$endPage = ceil($allNum / $pageSize); //总页数
    @$array = news($pageNum, $pageSize);


    //分页的函数
    function news($pageNum = 1, $pageSize = 3)
    {
        $array = array();
        $coon = mysqli_connect("localhost", "root", "");
        mysqli_select_db($coon, "xiangmu");
        mysqli_set_charset($coon, "utf8");

        // limit为约束显示多少条信息，后面有两个参数，第一个为从第几个开始，第二个为长度
        $rs = "select * from student inner join Class on Student.ClassNo=Class.ClassNo limit " . (($pageNum - 1) * $pageSize) . "," . $pageSize;
        $r = mysqli_query($coon, $rs);
        while ($obj = mysqli_fetch_object($r)) {
            $array[] = $obj;
        }

        //  mysqli_close($coon,"zyq");
        return $array;
    }



    //显示总页数的函数
    function allNews()
    {
        $coon = mysqli_connect("localhost", "root", "");
        mysqli_select_db($coon, "xiangmu");
        mysqli_set_charset($coon, "utf8");
        $rs = "select count(*) num from Student inner join Class on Student.ClassNo=Class.ClassNo"; //可以显示出总页数
        $r = mysqli_query($coon, $rs);
        $obj = mysqli_fetch_object($r);
        // mysqli_close($coon,"zyq");
        return $obj->num;
    }


    //定义SQL语句，查询所有学生信息
    $sql = "select * from Student inner join Class on Student.ClassNo=Class.ClassNo limit " . (($pageNum - 1) * $pageSize) . "," . $pageSize;

    //执行SQL语句，返回结果，并用表格显示信息
    if ($result = mysqli_query($db, $sql)) {
        echo "<form action ='UpdDel.php' method='post'>";
        echo "<input type='submit' name='btnDel' value='删除' onclick='return confirm(\"确定要删除选中的学生信息吗?\");'/>";
        echo "<input type='submit' name='btnUpdate' value='编辑'/>";
        echo "<table class='tb'><tr><th></th><th>学号</th><th>姓名</th><th>班级</th><th>性别</th><th>爱好</th><th>手机</th><th>电子邮箱</th><th>相片</th></tr>";

        //获取数据
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<tr><td><input type='checkbox' name='sel[]' value='" . $row["studentID"] . "'/>" .
                "</td><td>" . $row["studentID"] .
                "</td><td>" . $row["studentName"] .
                "</td><td>" . $row["classNo"] .
                "</td><td>" . $row["sex"] .
                "</td><td>" . $row["hobby"] .
                "</td><td>" . $row["mobile"] .
                "</td><td>" . $row["email"] .
                "</td><td><img src='" . $row["photo"] . "' width='35px'/>" .
                "</td></tr>";
        }
        echo "</table>";
        echo "</form>";

        //释放结果集
        $result->close();
    }

    //关闭连接
    $db->close();
    ?>

    <div>
        
        <a href="?pageNum=<?php echo $pageNum == 1 ? 1 : ($pageNum - 1) ?>" rel="external nofollow" rel="external nofollow">上一页</a>
        <a href="?pageNum=<?php echo $pageNum == $endPage ? $endPage : ($pageNum + 1) ?>" rel="external nofollow" rel="external nofollow">下一页</a>
        
        
    </div>

    <?php
    include 'Footer.html';  //包含页脚页
    ?>

</body>

</html>